﻿Imports lbGlobalcom
Imports Oracle.DataAccess.Client
Public Class fListaPlanesRegistrados
    Dim daSedes As lbGlobalcom.dsGlobalTableAdapters.GLO_SEDESTableAdapter
    Dim daTipoDoc As lbGlobalcom.dsGlobalTableAdapters.GLO_DATOSTABLASTableAdapter
    Private lTipoMovimi As String = ""
    Private lTipoDoc As String = ""
    Private lNumeroDoc As Integer = 0
    Private lSubClase As String = "PLA"
    Private lTercero As String = ""
    Private lSucursal As Integer = -1
    Public ReadOnly Property mTipoDoc() As String
        Get
            Return lTipoDoc
        End Get
    End Property
    Public ReadOnly Property mNumeroDoc() As Integer
        Get
            Return lNumeroDoc
        End Get
    End Property
    Public Property TipoMovimi() As String
        Get
            Return lTipoMovimi
        End Get
        Set(ByVal value As String)
            lTipoMovimi = value
        End Set
    End Property

    Public Property SubClase() As String
        Get
            Return lSubClase
        End Get
        Set(ByVal value As String)
            lSubClase = value
        End Set
    End Property

    Public Property mTercero() As String
        Get
            Return lTercero
        End Get
        Set(ByVal value As String)
            lTercero = value
        End Set
    End Property
    Public Property mSucursal() As Integer
        Get
            Return lSucursal
        End Get
        Set(ByVal value As Integer)
            lSucursal = value
        End Set
    End Property


    Private Sub btnConsultar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConsultar.Click
        Dim conn As OracleConnection = New OracleConnection(My.Settings.ConnectionString)
        Dim cmd As OracleCommand = conn.CreateCommand
        Dim qFrom As String = " FROM MOV_PLAN, TER_TERCERO, GLO_TIPODOCUMENTO "
        Dim qWhere As String = " WHERE MOV_PLAN.FECHADOCUMENTO BETWEEN :PDESDEF AND :PHASTAF AND GLO_TIPODOCUMENTO.DOCES = :PDOCES AND MOV_PLAN.IDDOCUMENTO = GLO_TIPODOCUMENTO.TIPODOC AND MOV_PLAN.IDTERCERO = TER_TERCERO.IDTERCERO(+) "
        Dim qSelect As String = "SELECT MOV_PLAN.IDDOCUMENTO, MOV_PLAN.NUMERO, MOV_PLAN.FECHADOCUMENTO,MOV_PLAN.IDTERCERO, MOV_PLAN.SUCURSAL "
        Dim qOrder As String = " ORDER BY MOV_PLAN.NUMERO"
        If cbSede.SelectedValue <> "00000" Then
            qWhere &= " AND MOV_PLAN.IDSEDE='" & cbSede.SelectedValue & "'"
        End If
        If txtNumerO.Text.Length > 0 Then
            qWhere &= " AND MOV_PLAN.NUMERO='" & txtNumero.Text & "'"
        End If
        cmd.CommandText = qSelect + " " + qFrom + " " + qWhere + " " + qOrder
        cmd.Parameters.Add("PDESDEF", OracleDbType.Date).Value = dtpFechaInicial.Value.Date
        cmd.Parameters.Add("PHASTAF", OracleDbType.Date).Value = dtpFechaFinal.Value.Date
        cmd.Parameters.Add("PDOCES", OracleDbType.Int16).Value = cbDocumento.SelectedValue
        Try
            conn.Open()
            DsMovimiento.MOV_PLAN.Rows.Clear()
            'DSMO()
            DsMovimiento.MOV_PLAN.Load(cmd.ExecuteReader)
            conn.Close()
            MOV_PLANDataGridView.Focus()
        Catch ex As Exception
            lbGlobalcom.cError.SalvaError(ex, "Samuel")
            MessageBox.Show(ex.Message)
        End Try

    End Sub

    Private Sub fListaPlanesRegistrados_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            daTipoDoc = New lbGlobalcom.dsGlobalTableAdapters.GLO_DATOSTABLASTableAdapter
            For Each SubC As String In lSubClase.Split(",")
                Dim dtTemGlo As lbGlobalcom.dsGlobal.GLO_DATOSTABLASDataTable = daTipoDoc.GetDataByIdGrupoSubclase("td", SubC)
                DsGlobal.GLO_DATOSTABLAS.Merge(dtTemGlo)
            Next
            If lTipoMovimi.Trim.Length > 0 Then
                Me.cbDocumento.Text = lTipoMovimi
                cbDocumento.Enabled = False
            End If
            dtpFechaInicial.Value = dtpFechaFinal.Value.AddDays(-30)
            daSedes = New lbGlobalcom.dsGlobalTableAdapters.GLO_SEDESTableAdapter
            daSedes.Fill(DsGlobal.GLO_SEDES)
            Dim drSed As lbGlobalcom.dsGlobal.GLO_SEDESRow = DsGlobal.GLO_SEDES.NewRow
            drSed.IDSEDE = "00000"
            drSed.NOMBRE = "Todas"
            DsGlobal.GLO_SEDES.Rows.Add(drSed)
            'cbSedes.SelectedValue = "00000"
            cbSede.SelectedValue = CustomPrincipal.GetSede
            cbSede.Enabled = False
        Catch ex As Exception
            lbGlobalcom.cError.SalvaError(ex, "Henry")
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub btnCancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancelar.Click
        Me.DialogResult = Windows.Forms.DialogResult.Cancel
    End Sub

    Private Sub AsignaMovimi(ByVal Fila As Integer)
        Try
            If Fila >= 0 Then
                lTipoDoc = MOV_PLANDataGridView.CurrentRow.Cells("IDDOCUMENTO").Value
                lNumeroDoc = MOV_PLANDataGridView.CurrentRow.Cells("NUMERO").Value
                Me.DialogResult = Windows.Forms.DialogResult.OK
            End If
        Catch ex As Exception
            lbGlobalcom.cError.SalvaError(ex, "Samuel")
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub MOV_PLANDataGridView_CellMouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles MOV_PLANDataGridView.CellMouseDoubleClick
        Try
            AsignaMovimi(e.RowIndex)
        Catch ex As Exception
            lbGlobalcom.cError.SalvaError(ex, "Henry")
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub MOV_PLANDataGridView_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MOV_PLANDataGridView.KeyDown
        If e.KeyData = Keys.Enter Then
            AsignaMovimi(MOV_PLANDataGridView.CurrentRow.Index)
        End If
    End Sub
End Class